<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .wrap {
            width: 1226px;
            margin: 0 auto;
            margin-top: 50px;
            position: relative;
        }

        nav {
            display: flex;
            position: absolute;
            right: 40px;
            bottom: 20px;
        }

        nav li {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            margin: 0 6px;
            flex: 1;
            border: 1px solid #333;
            background-color: rgba(0, 0, 0, .4);
        }

        nav .active {
            background-color: rgba(255, 255, 255, 0.4);
        }

        section {
            /* height: 400px; */
            line-height: 400px;
            text-align: center;
            border-top: 0;
        }

        section>div {
            display: none;
        }

        section>div>img {
            width: 100%;
        }

        section .show {
            display: block;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <nav>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </nav>
        <section>
            <div class="show"><img src="../images/1.jpg" alt=""></div>
            <div><img src="../images/2.webp" alt=""></div>
            <div><img src="../images/3.webp" alt=""></div>
            <div><img src="../images/4.webp" alt=""></div>
        </section>
    </div>
</body>
<script>
    // 导航栏切换 
    // 点击每一个li =>   当前li 背景颜色变天蓝色 字体变白色

    // 方法2: 先清除所有导航的活跃状态 在把当前被点击的元素的变为活跃状态


    var liList = document.querySelectorAll("nav li");
    console.log(liList);

    var partList = document.querySelectorAll("section>div");
    console.log(partList);


    for (var i = 0; i < liList.length; i++) { // 循环绑定事件 
        var li = liList[i]; // 遍历每个li
        li.setAttribute("data-index", i);

        partList[i].style.backgroundColor = randColor();

        li.onclick = function () { //给遍历的li绑定事件 

            var index = this.dataset.index; // 导航对应的下标

            for (var j = 0; j < liList.length; j++) { //  先清除所有导航的活跃状态,让所有part盒子隐藏
                liList[j].className = "";
                partList[j].className = "";
            }

            liList[index].className = "active";  //当前被点击的元素 变为活跃状态
            partList[index].className = "show";
        }
    }



    function randColor() {
        var colorStr = "0123456789abcdef";  // 16 
        var str = "#";
        for (var i = 0; i < 6; i++) {
            // var index = Math.round(Math.random() * (colorStr.length - 1));
            var index = Math.floor(Math.random() * colorStr.length);
            var char = colorStr.charAt(index);
            str += char;
        }
        console.log(str);
        return str;
    }




</script>

</html>